package com.mz.ozy.activity.me;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.mz.ozy.R;
import com.mz.ozy.base.BaseActivity;
import com.mz.ozy.bean.EventBean;
import com.mz.ozy.bean.RechargeRecordBean;
import com.mz.ozy.constants.Constants;
import com.mz.ozy.mvp.presenter.RechargeRecordPresenter;
import com.mz.ozy.mvp.view.RechargeRecordContract;
import com.mz.ozy.utils.PayManager;
import com.mz.ozy.utils.ToastUtils;
import com.mz.ozy.utils.Utils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONObject;

import androidx.annotation.Nullable;

import java.math.BigDecimal;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author lifeStyle on 2020/4/2 0002.
 */
public class MeRechargeActivity extends BaseActivity<RechargeRecordContract.View, RechargeRecordPresenter> implements RechargeRecordContract.View {
    @BindView(R.id.pay_group)
    RadioGroup payGroup;
    @BindView(R.id.et_amt)
    EditText etAmt;
    private int tag = 1;
    @BindView(R.id.tv_balance)
    TextView tvBalance;
    private PayManager mPayManager;

    public static void go(Context activity, String userBalance) {
        Intent intent = new Intent(activity, MeRechargeActivity.class);
        intent.putExtra("balance", userBalance);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_me_recharge);
        ButterKnife.bind(this);
        initTitle();
        EventBus.getDefault().register(this);
        tvCenterTitle.setText("充值");
        mPayManager = new PayManager(this);

        tvBalance.setText("当前余额:" + Utils.decimalUtils(getIntent().getStringExtra("balance"), 2));
        payGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                View checkedView = group.findViewById(checkedId);
                tag = Integer.valueOf(checkedView.getTag().toString());

            }
        });
        etAmt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                Utils.handleEditTextUtils(s, etAmt);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void getRechargeRecordSuccess(RechargeRecordBean bean) {

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(EventBean event) {
        if ((event.getCode() == Constants.EventCode.WX_PAY_SUCCESS || event.getCode() == Constants.EventCode.ALIPAY_PAY_SUCCESS)
                && PayManager.pay_from == 4) {
            EventBus.getDefault().post(new EventBean(Constants.EventCode.RECHARGE_SUCCESS, null));
            finish();
        }
    }

    @Override
    public void getRechargeSuccess(JSONObject bean) {
        if (tag == 2) {  //微信支付
            mPayManager.handleWxPay(bean);
        } else if (tag == 1) {  //支付宝支付
            mPayManager.handleAliPay(bean.optString("zfb"));
        }

    }

    protected void onResume() {
        super.onResume();
        PayManager.pay_from = 4;
    }

    @OnClick(R.id.btn_comit)
    public void onViewClicked() {
        String amt = etAmt.getText().toString();
        if (TextUtils.isEmpty(amt)) {
            ToastUtils.showToast("请输入充值金额");
            return;
        }
        mPresenter.getRecharg(this, tag + "", amt);
    }
}
